package com.jcfy.project.module.client;

import com.jcfy.common.utils.oss.AliyunOss;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.framework.base.ClientBaseController;
import com.jcfy.framework.base.Error;
import com.jcfy.framework.config.ServerConfig;
import com.jcfy.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传
 *
 * @author jiangfeng
 * @date 2020-08-13
 */
@Api("文件上传")
@RestController
@RequestMapping("/client/sys")
public class UploadController extends ClientBaseController {

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("文件上传")
    @Log(title = "文件上传", businessType = BusinessType.OTHER)
    @PostMapping(value = "/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws Exception {
        try {
//            // 上传文件路径
//            String filePath = JcfyConfig.getUploadPath();
//            // 上传并返回新文件名称
//            String fileName = FileUploadUtils.upload(filePath, file);
//            String url = serverConfig.getUrl() + fileName;

            // 上传文件至阿里云
            String url = AliyunOss.aliOssUpload(file);

            AjaxResult result = AjaxResult.success();
            result.put("url", url);
            return result;
        } catch (Exception e) {
            logger.error("[文件上传][状态#异常][原因# {}]", e.getMessage());
            return AjaxResult.error(Error._100000, e.getMessage());
        }
    }

}
